// pages/dealerOrder-detail/dealerOrder-detail.js
var app = getApp();
Page({
    data:{
      order:"",
      orderDetail:[],
      yunPrice:"0.00"
    },
    onLoad:function(e){
      var that = this;
      var orderId = e.id;
      wx.hideShareMenu()
      console.log(orderId);
      if(!e.share){ 
        that.setData({ share: true }); 
      }
      if (app.globalData.iphone == true) { that.setData({ iphone: 'iphone' }) }
      that.data.orderId = orderId;
      that.setData({
        orderId: orderId
      });
    },
    onShow : function () {
      
      var that = this;
      wx.showLoading(           {
        title: '加载中...',
        mask: true});
      app.get(`dealerorder/get_dealer_order_by_dealerorderid?DealerOrderId=${this.data.orderId}`,res=>{
        wx.hideLoading();
        console.log(res.result)
        if(res&&res.code=="0"){
          that.setData({
            order:res.result,
            orderDetail:{goods:res.Detail}
          })
        }else{
          wx.showModal({
            title: '错误',
            content: res.msg,
            showCancel: false,
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateBack()
              }
            }          
          })
        }

      })
      // setTimeout(function () {
      //   wx.request({
      //     url: app.globalData.urls + '/order/detail',
      //     data: {
      //       token: app.globalData.token,
      //       id: that.data.orderId
      //     },
      //     success: (res) => {
      //       wx.hideLoading();
      //       if (res.data.code != 0) {
      //         wx.showModal({
      //           title: '错误',
      //           content: res.data.msg,
      //           showCancel: false
      //         })
      //         return;
      //       }
      //       that.setData({
      //         orderDetail: res.data.data
      //       });
      //     }
      //   })
        // wx.hideLoading();
        // var yunPrice = parseFloat(that.data.yunPrice);
        // var allprice = 0;
        // var goodsList = that.data.goodsList;
        // for (var i = 0; i < goodsList.length; i++) {
        //   allprice += parseFloat(goodsList[0].price) * goodsList[0].number;
        // }
        // that.setData({
        //   allGoodsPrice: allprice,
        //   yunPrice: yunPrice
        // });
      // }, 800)
    },
    wuliuDetailsTap:function(e){
      var orderId = e.currentTarget.dataset.id;
      var numberId = e.currentTarget.dataset.number;
      wx.navigateTo({
        url: "/pages/dealerLogistics/dealerLogistics?id=" + orderId + '&number=' + numberId
      })
    },
    confirmBtnTap:function(e){
      let that = this;
      let orderId = this.data.orderId;
      let formId = e.detail.formId;
      wx.showModal({
          title: '确认您已收到商品？',
          content: '',
          success: function(res) {
            if (res.confirm) {
              wx.showLoading(           {
              	title: '加载中...',
                mask: true});
              wx.request({
                url: app.globalData.urls + '/order/delivery',
                data: {
                  token: app.globalData.token,
                  orderId: orderId
                },
                success: (res) => {
                  if (res.data.code == 0) {
                    that.onShow();
                    // 模板消息，提醒用户进行评价
                    let postJsonString = {};
                    postJsonString.keyword1 = { value: that.data.orderDetail.orderInfo.orderNumber, color: '#173177' }
                    let keywords2 = '您已确认收货，期待您的再次光临！';
                    if (app.globalData.order_reputation_score) {
                      keywords2 += '立即好评，系统赠送您' + app.globalData.order_reputation_score +'积分奖励。';
                    }
                    postJsonString.keyword2 = { value: keywords2, color: '#173177' }
                    app.sendTempleMsgImmediately(app.siteInfo.assessorderkey , formId,
                      '/pages/order-detail/order-detail?id=' + orderId, JSON.stringify(postJsonString));
                  }
                }
              })
            }
          }
      })
    },
    submitReputation: function (e) {
      let that = this;
      let formId = e.detail.formId;
      let postJsonString = {};
      // postJsonString.token = app.globalData.token;
      // postJsonString.orderId = this.data.orderId;
      let reputations = [];
      // console.log(e);
      let i = 0;
      var commentList=[];
      console.log(this.data.orderDetail);
      for (let index = 0; index < this.data.orderDetail.goods.length; index++) {
        const element = this.data.orderDetail.goods[index];        
        element.Content=e.detail.value["goodReputationRemark" + i];
        if(!element.Content){
          wx.showModal({
            showCancel:false,
            content:"请填写评价内容"
          })
          return;
        }
        let item={
          "GoodsId":element.GoodsId,
          "CustId":app.globalData.uid ,
          "StandardId": element.StandardId,
          "GoodsName":element.StandardName,
          "StandardName": element.StandardName,
          "Thumb":  element.Thumb,
          "Images": "[]",
          "Content":  element.Content,
          "OrderId":this.data.order.OrderId
        }
        commentList.push(item);
      }


      console.log(commentList);
      app.postForm("/order/insert_order_comment",{Json:JSON.stringify(commentList[0])},res=>{
        console.log(res);
        if(res&&res.code=="0"){
          wx.showModal({
            content:"提交成功",
            success(){
              wx.navigateBack();
            }
          })
        }else{
          wx.showToast({
            title: res.msg,
          })
        }
      })      
    }
})